﻿using B.S.Orders.Domain;
using B.S.Orders.ErrorCord;
using B.S.Orders.Read.Api.Application.Commands.DispatchProductCommand;
using B.S.Orders.Read.Api.DTO;
using MediatR;
using SqlSugar;

namespace B.S.Orders.Read.Api.Application.CommandsHander.DispatchProductCommandHandler
{
    public class GetDispatchProductQueryCommandHandler : IRequestHandler<GetDispatchProductQueryCommand, APIResult<APIPaging<DispatchProductDTO>>>
    {
        private readonly ISqlSugarClient db;
        private readonly ILogger<GetDispatchProductQueryCommandHandler> logger;

        public GetDispatchProductQueryCommandHandler(ISqlSugarClient db, ILogger<GetDispatchProductQueryCommandHandler> logger)
        {
            this.db = db;
            this.logger = logger;
        }

        public async Task<APIResult<APIPaging<DispatchProductDTO>>> Handle(GetDispatchProductQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<DispatchProductDTO>> result = new APIResult<APIPaging<DispatchProductDTO>>();

            // 进行两表联查
            var query = db.Queryable<DispatchProduct, DispatchDetails>((dp, dd) => dp.DispatchProductId == dd.DispatchProductId)
                          .Select((dp, dd) => new DispatchProductDTO
                          {
                              DispatchTheme = dd.DispatchTheme,
                              DispatchProductNo = dd.DispatchProductNo,
                              AssociatedProjects = dp.AssociatedProjects,
                              ProductName = dd.ProductName,
                              ProductNumber = dd.ProductNumber,
                              Specifications = dd.Specifications,
                              Unit = dd.Unit,
                              OrderQuantity = dd.OrderQuantity,
                              DispatchedSum = dd.DispatchedSum,
                              DispatchedNumber = dd.DispatchedNumber,
                              ConstructionPlannedDate = dd.ConstructionPlannedDate,
                              ScheduledCompletionDate = dd.ScheduledCompletionDate,
                              Delivery = dd.Delivery,
                              DocumentDate = dp.DocumentDate,
                              DocumentMakers = dp.DocumentMakers,
                              DispatchProductId = dp.DispatchProductId
                          });
            if (!string.IsNullOrEmpty(request.AssociatedProjects))
            {
                query = query.Where(dp => dp.AssociatedProjects == request.AssociatedProjects);
            }
            if (!string.IsNullOrEmpty(request.DispatchTheme))
            {
                query = query.Where(dp => dp.DispatchTheme == request.DispatchTheme);
            }
            if (!string.IsNullOrEmpty(request.DispatchProductNo))
            {
                query = query.Where(dp => dp.DispatchProductNo == request.DispatchProductNo);
            }
            if (request.FriDelivery != null)
            {
                query = query.Where(dp => dp.Delivery >= request.FriDelivery);
            }
            if (request.EndDelivery != null)
            {
                query = query.Where(dp => dp.Delivery <= request.EndDelivery);
            }
            if (!string.IsNullOrEmpty(request.ProductName))
            {
                query = query.Where(dp => dp.ProductName == request.ProductName);
            }
            if (!string.IsNullOrEmpty(request.ProductNumber))
            {
                query = query.Where(dp => dp.ProductNumber == request.ProductNumber);
            }
            if (request.FriConstructionPlannedDate != null)
            {
                query = query.Where(dp => dp.ConstructionPlannedDate >= request.FriConstructionPlannedDate);
            }
            if (request.EndConstructionPlannedDate != null)
            {
                query = query.Where(dp => dp.ConstructionPlannedDate <= request.EndConstructionPlannedDate);
            }
            if (request.FriScheduledCompletionDate != null)
            {
                query = query.Where(dp => dp.ScheduledCompletionDate >= request.FriScheduledCompletionDate);
            }
            if (request.EndScheduledCompletionDate != null)
            {
                query = query.Where(dp => dp.ScheduledCompletionDate >= request.EndScheduledCompletionDate);
            }
            if (request.FriDocumentDate != null)
            {
                query = query.Where(dp => dp.DocumentDate >= request.FriDocumentDate);
            }
            if (request.EndDocumentDate != null)
            {
                query = query.Where(dp => dp.DocumentDate >= request.EndDocumentDate);
            }
            if (!string.IsNullOrEmpty(request.DocumentMakers))
            {
                query = query.Where(dp => dp.DocumentMakers == request.DocumentMakers);
            }

            int TotalCount = query.Count();
            int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
            query = query.OrderByDescending(dp => dp.DispatchProductId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

            APIPaging<DispatchProductDTO> paging = new APIPaging<DispatchProductDTO>();
            paging.TotalCount = TotalCount;
            paging.TotalPage = TotalPage;
            paging.list = query.ToList();


            logger.LogInformation("查询派工明细表成功");
            result.Code = APIEnum.success;
            result.Msg = "查询成功";
            result.Data = paging;

            return result;
        }
    }
}